import { getYunWeiTasks } from '@/api/table'

export default {
  namespaced: true,
  state: {
    tableData: [],
    total: 0,
    totalPage: 0,
    dialogFormVisible: false,
    dialogDetailVisible: false,
    type: 1,
    rowData: {}
  },
  actions: {
    async getTasks({ commit }, params) {
      try {
        const res = await getYunWeiTasks(params)
        // console.log(res)
        const list = res.currentPageRecords
        list.forEach((it) => {
          it.method = '手动'
        })
        commit('setTableDate', list)
        const total = parseInt(res.totalCount)
        commit('setTotal', total)
        commit('setTotalPage', res.totalPage)
      } catch (error) {
        throw new Error(error)
      }
    }
  },
  mutations: {
    setTableDate(state, list) {
      state.tableData = list
    },
    setTotal(state, total) {
      state.total = total
    },
    setTotalPage(state, totalPage) {
      state.totalPage = totalPage
    },
    openDetail(state) {
      state.dialogDetailVisible = true
    },
    closeDetail(state) {
      state.dialogDetailVisible = false
    },
    openForm(state) {
      state.dialogFormVisible = true
    },
    closeForm(state) {
      state.dialogFormVisible = false
    },
    setType(state, type) {
      state.type = type
    },
    setRowDate(state, rowData) {
      state.rowData = rowData
    },
    clearRowDate(state) {
      state.rowData = {}
    }
  },
  getters: {
    imgSrc(state) {
      if (state.type === 1) {
        return 'https://likede2-admin.itheima.net/img/pic_1.834b274c.png'
      } else if (state.type === 3) {
        return 'https://likede2-admin.itheima.net/img/pic_3.e8208e34.png'
      } else if (state.type === 4) {
        return 'https://likede2-admin.itheima.net/img/pic_4.3b5af41c.png'
      }
    },
    iconSrc(state) {
      if (state.type === 1) {
        return ''
      } else if (state.type === 3) {
        return ''
      } else if (state.type === 4) {
        return ''
      }
    },
    dialogState(state) {
      if (state.type === 1) {
        return '代办'
      } else if (state.type === 2) {
        return '进行'
      } else if (state.type === 3) {
        return '取消'
      } else {
        return '完成'
      }
    },
    buttonName(state) {
      if (state.type === 1) {
        return '取消工单'
      }
      return '重新创建'
    }
  }
}
